package com.atguigu.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author liking
 * @Description
 * @Date 2025/4/29
 * @Version 1.0
 */
public class DictExcelListener extends AnalysisEventListener<DictEeVo> {
    private static final int BATCH_COUNT = 1000;
    private List<DictEeVo> cacheDataList = new ArrayList<>();
    private DictMapper dictMapper;


    public DictExcelListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }



    private void saveData(List<DictEeVo> cacheDataList) {
        dictMapper.insertBatch(cacheDataList);
    }

    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
       cacheDataList.add(dictEeVo);
        if (cacheDataList.size() >=  BATCH_COUNT) {
            saveData(cacheDataList);
            cacheDataList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData(cacheDataList);
    }
}
